package com.shuoyue.adven.main.bussiness;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.shuoyue.adven.R;
import com.shuoyue.adven.entity.Adv;
import com.shuoyue.adven.view.XProportionImageView;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import lombok.Data;
import lombok.EqualsAndHashCode;


@Data
@EqualsAndHashCode(callSuper = false)

public class BusinessDataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    List<Adv> mDatas;
    private Context mContext;
    LayoutInflater inflater;
    private List<View> mViewPoolVideo = new ArrayList<>();
    private List<View> mViewPoolImg = new ArrayList<>();
    private List<View> mViewPoolText = new ArrayList<>();

    public BusinessDataAdapter(Context context, List<Adv> mDatas) {
        if (mDatas == null) {
            mDatas = new ArrayList<>();
        }
        this.mDatas = mDatas;
        this.mContext = context;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getItemViewType(int position) {
        if (mDatas == null || mDatas.size() == 0) {
            return 0;
        }
        Adv b = mDatas.get(position % mDatas.size());
        if (b.getType() == Adv.TYPE_RICH_TEXT) {
            return 0;
        } else if (b.getType() == Adv.TYPE_IMG) {
            return 1;
        } else if (b.getType() == Adv.TYPE_VIDEO) {
            return 2;
        }
        return 0;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {

            View itemView = inflater.inflate(R.layout.item_business_text, parent, false);
            ViewHolderText viewholder = new ViewHolderText(itemView);
            return viewholder;
        } else if (viewType == 1) {
            View itemView = inflater.inflate(R.layout.item_business_img, parent, false);
            ViewHolderImg viewholder = new ViewHolderImg(itemView);
            return viewholder;
        } else if (viewType == 2) {
            View itemView = inflater.inflate(R.layout.item_business_video, parent, false);
            ViewHolderVideo viewholder = new ViewHolderVideo(itemView);
            return viewholder;
        }
        return null;
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (mDatas == null || mDatas.size() == 0) {
            return;
        }
        Adv homeData = mDatas.get(position % mDatas.size());
        if (holder instanceof ViewHolderText) {
            ((ViewHolderText) holder).advName.setText(TextUtils.isEmpty(homeData.getTitle()) ? "" : homeData.getTitle());
            ((ViewHolderText) holder).advText.setText(TextUtils.isEmpty(homeData.getFontContent()) ? "" : homeData.getFontContent());
            if (mContext != null) {
                Glide.with(mContext)
                        .load(homeData.getLogo())
                        .error(R.mipmap.img_loading_gray)
                        .placeholder(R.mipmap.img_loading_gray)
                        .into(((ViewHolderText) holder).headImg);
            }
            if (homeData.getFontStyle() == 1) {//1：红底白字 2：蓝底白字 3：绿底黑字 4：黄底黑字
                ((ViewHolderText) holder).bgCard.setBackgroundColor(ContextCompat.getColor(mContext, R.color.red));
                ((ViewHolderText) holder).advText.setTextColor(ContextCompat.getColor(mContext, R.color.white));
            }
            if (homeData.getFontStyle() == 2) {//1：红底白字 2：蓝底白字 3：绿底黑字 4：黄底黑字
                ((ViewHolderText) holder).bgCard.setBackgroundColor(ContextCompat.getColor(mContext, R.color.blue));
                ((ViewHolderText) holder).advText.setTextColor(ContextCompat.getColor(mContext, R.color.white));
            }
            if (homeData.getFontStyle() == 3) {//1：红底白字 2：蓝底白字 3：绿底黑字 4：黄底黑字
                ((ViewHolderText) holder).bgCard.setBackgroundColor(ContextCompat.getColor(mContext, R.color.green));
                ((ViewHolderText) holder).advText.setTextColor(ContextCompat.getColor(mContext, R.color.black));
            }
            if (homeData.getFontStyle() == 4) {//1：红底白字 2：蓝底白字 3：绿底黑字 4：黄底黑字
                ((ViewHolderText) holder).bgCard.setBackgroundColor(ContextCompat.getColor(mContext, R.color.yellow));
                ((ViewHolderText) holder).advText.setTextColor(ContextCompat.getColor(mContext, R.color.black));
            }
            if (!TextUtils.isEmpty(homeData.getTextColor()) && !TextUtils.isEmpty(homeData.getTextColor())) {
                ((ViewHolderText) holder).bgCard.setBackgroundColor(Color.parseColor("#" + homeData.getBackGroundColor()));
                ((ViewHolderText) holder).advText.setTextColor(Color.parseColor("#" + homeData.getTextColor()));
            }

        } else if (holder instanceof ViewHolderImg) {
            ((ViewHolderImg) holder).advName.setText(TextUtils.isEmpty(homeData.getTitle()) ? "" : homeData.getTitle());
            if (mContext != null) {
                Glide.with(mContext)
                        .load(homeData.getImgUrl())
                        .error(R.mipmap.img_loading_gray)
                        .placeholder(R.mipmap.img_loading_gray)
                        .into(((ViewHolderImg) holder).advImg);
                Glide.with(mContext)
                        .load(homeData.getLogo())
                        .error(R.mipmap.img_loading_gray)
                        .placeholder(R.mipmap.img_loading_gray)
                        .into(((ViewHolderImg) holder).headImg);
            }
        } else if (holder instanceof ViewHolderVideo) {
            ((ViewHolderVideo) holder).advName.setText(TextUtils.isEmpty(homeData.getTitle()) ? "" : homeData.getTitle());
            if (mContext != null) {
                Glide.with(mContext)
                        .load(homeData.getVideoUrl())
                        .error(R.mipmap.img_loading_gray)
                        .placeholder(R.mipmap.img_loading_gray)
                        .into(((ViewHolderVideo) holder).advImg);
                Glide.with(mContext)
                        .load(homeData.getLogo())
                        .error(R.mipmap.img_loading_gray)
                        .placeholder(R.mipmap.img_loading_gray)
                        .into(((ViewHolderVideo) holder).headImg);
            }
        }
    }

    public void needScroll(boolean needScroll) {
        this.needScroll = needScroll;
//        getItemCount();
    }

    boolean needScroll = true;

//    public int ItemCount = 0;

    @Override
    public int getItemCount() {
        if (mDatas == null || mDatas.size() == 0) {
            return 0;
        } else if (needScroll) {
            return Integer.MAX_VALUE;
        } else {
            return mDatas.size();
        }
//        return ItemCount;
    }

    public void updateDatas(List<Adv> newData) {
        if (mDatas == null) {
            mDatas = new ArrayList<>();
            if (newData != null)
                mDatas.addAll(newData);
        } else {
            mDatas.clear();
            mDatas.addAll(newData);
        }
    }


    static class ViewHolderText extends RecyclerView.ViewHolder {
        @BindView(R.id.head_img)
        ImageView headImg;
        @BindView(R.id.adv_name)
        TextView advName;
        @BindView(R.id.adv_text)
        TextView advText;
        @BindView(R.id.bg_card)
        FrameLayout bgCard;

        ViewHolderText(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static
    class ViewHolderImg extends RecyclerView.ViewHolder {
        @BindView(R.id.head_img)
        ImageView headImg;
        @BindView(R.id.adv_name)
        TextView advName;
        @BindView(R.id.adv_img)
        XProportionImageView advImg;

        ViewHolderImg(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static
    class ViewHolderVideo extends RecyclerView.ViewHolder {
        @BindView(R.id.head_img)
        ImageView headImg;
        @BindView(R.id.adv_name)
        TextView advName;
        @BindView(R.id.adv_img)
        XProportionImageView advImg;

        ViewHolderVideo(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public void resetData(List<Adv> data) {
        if (mDatas == null) {
            mDatas = new ArrayList<>();
        }
        mDatas.clear();
        mDatas.addAll(data);
    }
}
